﻿using System;
using System.Collections.Specialized;
using MonitorProvider.Prototype;
using System.Data;
using FileMonitor.Configuration;
using FileMonitor.Configuration.Prototype;
using FileMonitor.Configuration.Providers;
using System.Collections.Generic;
using FileMonitor.Service.Prototype;
using FileMonitor.Configuration.Prototype.BizProcess;
namespace XLSMonitor
{
    public class Process:FileMonitorBase
    {

         public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {

            base.Initialize(name, config);
        }
        public override void ProcessCreated(System.IO.FileSystemEventArgs e)
        {
            try
            {
                string filename = e.Name;
                string filterPlantCode = base.baseConfiguration.FilterPlantCode;
                string plantCode = this.GetPlantCodeFromFileName(e.Name);

                if (string.IsNullOrEmpty(filterPlantCode))
                {
                    ProcessFile(filename, e.FullPath);
                }
                else
                {
                    string[] filterPlants = filterPlantCode.Split(",".ToCharArray());
                    if (filterPlants != null)
                    {
                        if (filterPlants.Length == 0)
                        {
                            ProcessFile(filename, e.FullPath);
                        }
                        else
                        {
                            if (!filterPlantCode.Contains(plantCode))
                            {
                                ProcessFile(filename, e.FullPath);
                            }
                        }

                    }
                    else
                    {
                        ProcessFile(filename, e.FullPath);
                    }
                }

            }

             catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public override void ProcessChanged(System.IO.FileSystemEventArgs e)
        {
        
        }

        public override void ProcessDeleted(System.IO.FileSystemEventArgs e)
        {

        }

        #region 
        private void ProcessFile(string filename, string filePath)
        {
            string plantCode = GetPlantCodeFromFileName(filename);

            List<PlantProcessConfiguration> listPlantConfigs = ProviderService.GetPlantProcessConfigProvider().GetPlantProcessConfiguration(this.exchangeDB, plantCode, base.baseConfiguration.DataType);
             
            try
            {
                #region Defaut Process
                if (listPlantConfigs.Count == 0)
                {
                    DBProcess(null, plantCode, filename, filePath);
                }
                #endregion

                foreach (PlantProcessConfiguration plantConfig in listPlantConfigs)
                {

                    #region FTP Process
                    if (plantConfig.ProcessType == ProcessType.FTP)
                    {
                        FTPProcess(plantConfig, plantCode, filename, filePath);
                    }
                    #endregion

                    #region ExchangeDB Process
                    if (plantConfig.ProcessType == ProcessType.ExChangeDB)
                    {
                        DBProcess(plantConfig, plantCode, filename, filePath);
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void DBProcess(PlantProcessConfiguration plantConfig, string plantCode, string filename, string filePath)
        {
            try
            {
                IProcessFile process = ProcessFactory.CreateProcess(ProcessType.ExChangeDB);
                ExcutedTask task = process.ProcessFile(plantConfig, base.baseConfiguration, plantCode, filename, filePath);
                NotifyTask(task);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void FTPProcess(PlantProcessConfiguration plantConfig, string plantCode, string filename, string filePath)
        {
            try
            {
                IProcessFile process = ProcessFactory.CreateProcess(ProcessType.FTP);
                ExcutedTask task = process.ProcessFile(plantConfig, base.baseConfiguration, plantCode, filename, filePath);
                NotifyTask(task);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Utility Method

        private string GetPlantCodeFromFileName(string FileName)
        {
            string plantCodeIndex = string.IsNullOrEmpty(base.baseConfiguration.PlantCodeIndex) ? "0" : base.baseConfiguration.PlantCodeIndex;
            string plantCodeLength = string.IsNullOrEmpty(base.baseConfiguration.PlantCodeLength) ? "4" : base.baseConfiguration.PlantCodeLength;
            string plantCode = FileName.Substring(Int32.Parse(plantCodeIndex), Int32.Parse(plantCodeLength));
            return plantCode;
        }

        #endregion

      
    }
}
